package com.tlias.coutroller;

import com.tlias.entity.*;
import com.tlias.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
// 【推荐】在类上统一添加路径前缀，让代码更简洁
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    // 分页条件查询: GET /clazzs
    @GetMapping
    public Result clazzList(ClazzQueryParam param) {
        log.info("分页查询班级信息: {}", param);
        PageBean pageBean = clazzService.clazzList(param);
        return Result.success(pageBean);
    }
    /**
     * 查询所有班级: GET /clazzs/list
     */
    @GetMapping("/list")
    public Result findAllList() {
        log.info("查询所有班级");
        List<Clazz> clazzList = clazzService.findAllList();
        return Result.success(clazzList);
    }

    // 根据id查询: GET /clazzs/{id}
    // 这个方法现在不会再和查询所有班级的方法冲突
    @GetMapping("/{id}")
    public Result getClazz(@PathVariable Integer id) {
        log.info("根据id查询班级信息: {}", id);
        Clazz clazz = clazzService.getClazz(id);
        return Result.success(clazz);
    }

    // 添加班级: POST /clazzs
    @PostMapping
    public Result insertClazz(@RequestBody Clazz clazz) {
        log.info("添加班级信息: {}", clazz);
        clazzService.insertClazz(clazz);
        return Result.success();
    }

    // 修改班级: PUT /clazzs
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        log.info("修改班级信息: {}", clazz);
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    // 删除班级: DELETE /clazzs/{id}
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id) {
        log.info("删除班级信息: {}", id);
        clazzService.deleteClazz(id);
        return Result.success();
    }
}